package com.homework;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author: XiangHu
 * @date: 2025年02月26日 15:20
 * @describe:
 */
public class Pet {
    public static void addPet(String nickname, String type, int age, String gender, double price) {
        String sql = "INSERT INTO h_pet (nickname, type, age, gender, price) VALUES (?, ?, ?, ?, ?)";
        JDBCUtils.update(sql, nickname, type, age, gender, price);
        System.out.println("宠物添加成功");
    }

    public static void updatePet(int petId, String nickname, String type, int age, String gender, double price) {
        String sql = "UPDATE h_pet SET nickname = ?, type = ?, age = ?, gender = ?, price = ? WHERE id = ?";
        JDBCUtils.update(sql, nickname, type, age, gender, price, petId);
        System.out.println("宠物信息更新成功");
    }

    public static void deletePet(int petId) {
        String sql = "DELETE FROM h_pet WHERE id = ?";
        JDBCUtils.update(sql, petId);
        System.out.println("宠物删除成功");
    }

    public static void viewPets(String orderBy) {
        String sql = "SELECT * FROM h_pet ORDER BY " + orderBy;
        ResultSet rs = null;
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            rs = stmt.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    System.out.println("宠物ID: " + rs.getInt("id"));
                    System.out.println("昵称: " + rs.getString("nickname"));
                    System.out.println("类型: " + rs.getString("type"));
                    System.out.println("年龄: " + rs.getInt("age"));
                    System.out.println("性别: " + rs.getString("gender"));
                    System.out.println("价格: " + rs.getDouble("price"));
                    System.out.println("-----------------------------------");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 确保ResultSet被显式关闭
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void buyPet(int petId, int userId) {
        String sql = "SELECT * FROM h_pet WHERE id = ?";
        ResultSet rs = null;
        try (Connection conn = JDBCUtils.getConnection())
              {
                  PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, petId);
            rs = stmt.executeQuery();

            if (rs != null && rs.next()) {
                // 确保购买宠物是否有人买
                if (rs.getInt("owner_id") == 0) {
                    double price = rs.getDouble("price");
                    String updatePetSql = "UPDATE h_pet SET owner_id = ? WHERE id = ?";
                    JDBCUtils.update(updatePetSql, userId, petId);

                    // 扣除用户的积分
                    String updateUserSql = "UPDATE h_user SET points = points - ? WHERE id = ?";
                    JDBCUtils.update(updateUserSql, price, userId);

                    System.out.println("购买成功！");
                    System.out.println("当前购买的用户ID: " + userId);
                    System.out.println("购买的宠物ID: " + petId);

                } else {
                    System.out.println("该宠物已经被购买！");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
